/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.authorization.domain.SysRole;
import cn.ibizlab.core.authorization.service.SysRoleService;
import cn.ibizlab.core.authorization.filter.SysRoleSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysRole] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysRoleResource {

    @Autowired
    public SysRoleService sysRoleService;

    @Autowired
    @Lazy
    public SysRoleDTOMapping sysRoleDtoMapping;

    @Autowired
    @Lazy
    public SysRoleFilterDTOMapping sysRoleFilterDtoMapping;


    /**
    * 创建Create 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"系统角色" },  notes = "SysRole-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Create-all') or hasPermission(this.sysRoleDtoMapping.toDomain(#dto),'uaa-SysRole-Create')")
    @PostMapping(value = {"sys_roles","sys_roles"})
    public ResponseEntity<SysRoleDTO> create
            (@Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        sysRoleService.create(domain);
        SysRole rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 系统角色
    * 
    *
    * @param id id
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"系统角色" },  notes = "SysRole-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Get-all') or hasPermission(this.sysRoleDtoMapping.toDomain(returnObject.body),'uaa-SysRole-Get')")
    @GetMapping("sys_roles/{id}")
    public ResponseEntity<SysRoleDTO> getById
            (@PathVariable("id") String id) {
        SysRole rt = sysRoleService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 系统角色
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"系统角色" },  notes = "SysRole-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Remove-all') or hasPermission(this.sysRoleService.get(#id),'uaa-SysRole-Remove')")
    @DeleteMapping("sys_roles/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysRoleService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 系统角色
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"系统角色" },  notes = "SysRole-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Update-all') or hasPermission(this.sysRoleService.get(#id),'uaa-SysRole-Update')")
    @VersionCheck(entity = "sysrole" , versionfield = "updateTime")
    @PutMapping("sys_roles/{id}")
    public ResponseEntity<SysRoleDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        domain.setId(id);
        sysRoleService.update(domain);
        SysRole rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * Cache_evict 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "Cache_evict", tags = {"系统角色" },  notes = "SysRole-Cache_evict ")
    @PostMapping("sys_roles/cache_evict")
    public ResponseEntity<SysRoleDTO> cacheEvict
            (@Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        SysRole rt = sysRoleService.cacheEvict(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"系统角色" },  notes = "SysRole-CheckKey ")
    @PostMapping("sys_roles/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        Integer rt = sysRoleService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"系统角色" },  notes = "SysRole-GetDraft ")
    @GetMapping("sys_roles/getdraft")
    public ResponseEntity<SysRoleDTO> getDraft
            (@SpringQueryMap SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        SysRole rt = sysRoleService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * PermissionAuth 系统角色
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "PermissionAuth", tags = {"系统角色" },  notes = "SysRole-PermissionAuth ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-PermissionAuth-all') or hasPermission(this.sysRoleDtoMapping.toDomain(#dto),'uaa-SysRole-PermissionAuth')")
    @PostMapping("sys_roles/{id}/permissionauth")
    public ResponseEntity<SysRoleDTO> permissionAuthById
            (@PathVariable("id") String id, @Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        domain.setId(id);
        SysRole rt = sysRoleService.permissionAuth(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * PersonAuth 系统角色
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "PersonAuth", tags = {"系统角色" },  notes = "SysRole-PersonAuth ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-PersonAuth-all') or hasPermission(this.sysRoleDtoMapping.toDomain(#dto),'uaa-SysRole-PersonAuth')")
    @PostMapping("sys_roles/{id}/personauth")
    public ResponseEntity<SysRoleDTO> personAuthById
            (@PathVariable("id") String id, @Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        domain.setId(id);
        SysRole rt = sysRoleService.personAuth(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysRoleDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"系统角色" },  notes = "SysRole-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Save-all') or hasPermission(this.sysRoleDtoMapping.toDomain(#dto),'uaa-SysRole-Save')")
    @PostMapping("sys_roles/save")
    public ResponseEntity<SysRoleDTO> save
            (@Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        sysRoleService.save(domain);
        SysRole rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * All 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleDTO>>
    */
    @ApiOperation(value = "All", tags = {"系统角色" },  notes = "SysRole-All ")
    @PostMapping("sys_roles/all")
    public ResponseEntity<List<SysRoleDTO>> all
            (@Validated @RequestBody SysRoleDTO dto) {
        SysRole domain = sysRoleDtoMapping.toDomain(dto);
        List<SysRole> rt = sysRoleService.all(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysRoleDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"系统角色" },  notes = "SysRole-FetchDefault ")
    @PostMapping("sys_roles/fetchdefault")
    public ResponseEntity<List<SysRoleDTO>> fetchDefault
            (@Validated @RequestBody SysRoleFilterDTO dto) {
        SysRoleSearchContext context = sysRoleFilterDtoMapping.toDomain(dto);
        Page<SysRole> domains = sysRoleService.searchDefault(context) ;
        List<SysRoleDTO> list = sysRoleDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchNav 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleDTO>>
    */
    @ApiOperation(value = "查询FetchNav", tags = {"系统角色" },  notes = "SysRole-FetchNav ")
    @PostMapping("sys_roles/fetchnav")
    public ResponseEntity<List<SysRoleDTO>> fetchNav
            (@Validated @RequestBody SysRoleFilterDTO dto) {
        SysRoleSearchContext context = sysRoleFilterDtoMapping.toDomain(dto);
        Page<SysRole> domains = sysRoleService.searchNav(context) ;
        List<SysRoleDTO> list = sysRoleDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchRoleByGroupId 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleDTO>>
    */
    @ApiOperation(value = "查询FetchRoleByGroupId", tags = {"系统角色" },  notes = "SysRole-FetchRoleByGroupId ")
    @PostMapping("sys_roles/fetchrolebygroupid")
    public ResponseEntity<List<SysRoleDTO>> fetchRoleByGroupId
            (@Validated @RequestBody SysRoleFilterDTO dto) {
        SysRoleSearchContext context = sysRoleFilterDtoMapping.toDomain(dto);
        Page<SysRole> domains = sysRoleService.searchRoleByGroupId(context) ;
        List<SysRoleDTO> list = sysRoleDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchRoleByUserId 系统角色
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysRoleDTO>>
    */
    @ApiOperation(value = "查询FetchRoleByUserId", tags = {"系统角色" },  notes = "SysRole-FetchRoleByUserId ")
    @PostMapping("sys_roles/fetchrolebyuserid")
    public ResponseEntity<List<SysRoleDTO>> fetchRoleByUserId
            (@Validated @RequestBody SysRoleFilterDTO dto) {
        SysRoleSearchContext context = sysRoleFilterDtoMapping.toDomain(dto);
        Page<SysRole> domains = sysRoleService.searchRoleByUserId(context) ;
        List<SysRoleDTO> list = sysRoleDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建系统角色
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Create-all')")
    @ApiOperation(value = "批量新建系统角色", tags = {"系统角色" },  notes = "批量新建系统角色")
	@PostMapping("sys_roles/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysRoleDTO> dtos) {
        sysRoleService.createBatch(sysRoleDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除系统角色
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Remove-all')")
    @ApiOperation(value = "批量删除系统角色", tags = {"系统角色" },  notes = "批量删除系统角色")
	@DeleteMapping("sys_roles/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysRoleService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新系统角色
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Update-all')")
    @ApiOperation(value = "批量更新系统角色", tags = {"系统角色" },  notes = "批量更新系统角色")
	@PutMapping("sys_roles/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysRoleDTO> dtos) {
        sysRoleService.updateBatch(sysRoleDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存系统角色
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Save-all')")
    @ApiOperation(value = "批量保存系统角色", tags = {"系统角色" },  notes = "批量保存系统角色")
	@PostMapping("sys_roles/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysRoleDTO> dtos) {
        sysRoleService.saveBatch(sysRoleDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入系统角色
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysRole-Save-all')")
    @ApiOperation(value = "批量导入系统角色", tags = {"系统角色" },  notes = "批量导入系统角色")
	@PostMapping("sys_roles/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysRoleDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysRoleService.importData(config,ignoreError,sysRoleDtoMapping.toDomain(dtos)));
    }

}
